package cn.jinjimi.app.custom.ui.view;

import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import cn.jinjimi.framework.helper.annotation.EventTrace;
import cn.jinjimi.app.R;
import cn.jinjimi.app.bean.EventID;
import cn.jinjimi.app.bean.QuesCode;
import cn.jinjimi.app.custom.ui.activity.CustomEditOrAddActivity;
import cn.jinjimi.app.custom.ui.activity.SelectCustomActivity;
import cn.jinjimi.app.store.entity.ClueEntity;

/**
 * Created by Roy
 * Date: 17/1/6
 */

public class CustomClueSaveMenuWindow extends PopupWindow implements View.OnClickListener {
    private Context mContext;
    private ClueEntity mClue;

    private RelativeLayout rlAdd;
    private RelativeLayout rlTransfer;

    public CustomClueSaveMenuWindow(Context context, ClueEntity clue) {
        this.mContext = context;
        this.mClue = clue;

        View view = View.inflate(context, R.layout.view_custom_clue_save_menu_layout, null);
        setContentView(view);

        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setOutsideTouchable(true);
        setFocusable(true);
        setTouchable(true);

        initView(view);
        initListener(view);
    }

    private void initView(View view) {
        rlAdd = (RelativeLayout) view.findViewById(R.id.rl_add);
        rlTransfer = (RelativeLayout) view.findViewById(R.id.rl_transfer);
    }

    private void initListener(View view) {
        view.findViewById(R.id.rl_add).setOnClickListener(this);
        view.findViewById(R.id.rl_transfer).setOnClickListener(this);
        view.findViewById(R.id.btn_save).setOnClickListener(this);

        view.findViewById(R.id.more_menu_bg).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_add: // 新增客户
                enterAddCustomer();
                break;
            case R.id.rl_transfer: // 关联已有客户
                enterBindCustomer();
                break;
        }
        dismiss();
    }

    @EventTrace(EventID.APP_CUSTOMER_EDITDETAIL_ADD)
    private void enterBindCustomer() {
        SelectCustomActivity.launch((Activity) mContext, mClue, QuesCode.QuestSelectTranferCode);
    }

    @EventTrace(EventID.APP_CUSTOMER_EDITDETAIL_CREATE)
    private void enterAddCustomer() {
        CustomEditOrAddActivity.launch(mContext, mClue);
    }

    public void beginAnimate() {
        ObjectAnimator.ofFloat(rlAdd, ViewGroup.TRANSLATION_Y, mContext.getResources().getDimension(R.dimen.dp_80), 0).setDuration(300).start();
        ObjectAnimator.ofFloat(rlTransfer, ViewGroup.TRANSLATION_Y, mContext.getResources().getDimension(R.dimen.dp_40), 0).setDuration(200).start();
    }
}
